<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Box\Models\BoxOrderModel;
use Summer\Hjd\Models\CurrencyModel;
use Summer\Hjd\Models\DetailedBonusModel;
use Summer\Hjd\Models\DetailedCashModel;
use Summer\Hjd\Models\DetailedHeartModel;
use Summer\Hjd\Models\DetailedLineModel;
use Summer\Hjd\Models\DetailedScoreModel;
use Summer\Hjd\Models\DetailedStockModel;
use Summer\Hjd\Models\WalletModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Config;
use Db;
use Exception;


class DetailedController extends Controller
{
    use ApiReturn;

    /**
     * 财务流水
     * @param JWTAuth $auth
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page = Input::get('page', 1);
        $page_size = Input::get('page_size', 20);
        $currency_slug=Input::get('currency_slug','bonus');

        if(!$currency_slug){
            $currency_slug='bonus';
        }
        //币种信息-余额信息-收支信息
        $currency=CurrencyModel::with([
                'currency_rgs'=>function($qr){
                    $qr->where('status',1);
                },
                'currency_wds'=>function($qw){
                    $qw->where('status',1);
                }
            ])
            ->where('slug',$currency_slug)->where('status',1)->first();
        if(!$currency){
            return $this->apiError('币种信息获取失败');
        }
        $wallet=WalletModel::where('user_id',$user->id)->where('currency_slug',$currency_slug)->first();

        if($currency_slug=='bonus'){
            $query=DetailedBonusModel::select();
        }
        if($currency_slug=='cash'){
            $query=DetailedCashModel::select();
        }
        if($currency_slug=='score'){
            $query=DetailedScoreModel::select();
        }
        if($currency_slug=='heart'){
            $query=DetailedHeartModel::select();
        }
        if($currency_slug=='stock'){
            $query=DetailedStockModel::select();
        }
        if($currency_slug=='line'){
            $query=DetailedLineModel::select();
        }
        $query->with([
            'relate'=>function($q){
                $q->select('id','username','userphone','realname','nickname');
            },
            'change'
        ]);
        $query->where('user_id',$user->id);
        if(Input::has('start_date')){
            $start_date=Input::get('start_date');
            $query->whereDate('created_at','>=',$start_date);
        }
        if(Input::has('end_date')){
            $end_date=Input::get('end_date');
            //结束时间应该算到当天的23:59:59也就是第二天的0点。
            $end_date=date('Y-m-d H:i:s',strtotime("$end_date+1day"));
            $query->whereDate('created_at','<=',$end_date);
        }
        if(Input::has('inout')){
            $inout=Input::get('inout');
            if($inout=='in'){
                $query->where('inout',1);
            }
            if($inout=='out'){
                $query->where('inout',0);
            }
        }
        if(Input::has('change_slug')){
            $change_slug=Input::get('change_slug');
            $query->where('change_slug',$change_slug);
        }

        $query->orderBy('id','DESC');
        $records=$query->paginate($page_size,$page);

//        //for-jikj1008
//        $jrsy_pre_money=BoxOrderModel::where('buy_uid',$user->id)->whereDate('created_at',Carbon::today())->where('status',4)->sum('pre_money');
//
//        $ljsy_bonus=DetailedBonusModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_zhitui')->sum('money');
//        $ljsy_score=DetailedScoreModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_jiantui')->sum('money');
//        $ljsy_stock=DetailedStockModel::where('user_id',$user->id)->where('inout',1)->whereIn('change_slug',['bonus_jicha','bonus_fenhong'])->sum('money');
//        $ljsy=$ljsy_bonus+$ljsy_score+$ljsy_stock+$jrsy_pre_money;
//        $return['ljsy']=$ljsy;

        $return['records']=$records;
        $return['currency']=$currency;
        $return['wallet']=$wallet;
        return $this->apiSuccess('操作成功',$return);

    }

}
